﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace MvcControlsToolkit
{
    public class LegendOptions: BaseObject
    {
        private string rendererOptions
        {
            get { return Attributes["rendererOptions"]; }
            set { Attributes["rendererOptions"] = value; }
        }

        /// <summary>
        /// Wether to display the legend on the graph
        /// </summary>
        public bool Show
        {
            get
            {
                if (Attributes.ContainsKey("show"))
                    return Boolean.Parse(GetString("show"));
                else
                    return true;
            }

            set
            {
                Attributes["show"] = value.ToString().ToLower();
            }
        }        

        /// <summary>
        /// Placement of the legend. 
        /// </summary>
        public Position Position{
            get
            {
                switch (GetString("location"))
                {
                    case "s": return Position.Bottom;
                    case "w": return Position.Left;
                    case "n": return Position.Top;
                    case "e": return Position.Right;
                    default: return Position.Right;
                }
            }
            set{
                switch(value)
                {
                    case Position.Bottom: SetString("location", "s"); break;
                    case Position.Left: SetString("location", "w"); break;
                    case Position.Top: SetString("location", "n"); break;
                    case Position.Right: SetString("location", "e"); break;
                    case Position.TopRight: SetString("location", "ne"); break;
                    case Position.TopLeft: SetString("location", "nw"); break;
                    case Position.BottomRight: SetString("location", "se"); break;
                    case Position.BottomLeft: SetString("location", "sw"); break;
                }
            }
        }

        /// <summary>
        /// Array of labels to use. 
        /// </summary>
        public string[] Labels{
            get{
                if (Attributes.ContainsKey("labels"))
                {
                    var json = new JavaScriptSerializer();
                    var result = json.Deserialize<string[]>(Attributes["labels"]);
                    return result;
                }
                else return null;
            }
            set
            {
                var json = new JavaScriptSerializer();
                var result = json.Serialize(value);
                Attributes["labels"] = result.ToLower();
            }
        }

        /// <summary>
        /// Show the label text on the legend.
        /// </summary>
        public bool ShowLabels
        {
            get
            {
                if (Attributes.ContainsKey("showLabels"))
                    return Boolean.Parse(GetString("showLabels"));
                else
                    return true;
            }

            set
            {
                Attributes["showLabels"] = value.ToString().ToLower();
            }
        }

        /// <summary>
        /// Show the color swatches on the legend.
        /// </summary>
        public bool ShowSwatch
        {
            get
            {
                if (Attributes.ContainsKey("showSwatch"))
                    return Boolean.Parse(GetString("showSwatch"));
                else
                    return true;
            }

            set
            {
                Attributes["showSwatch"] = value.ToString().ToLower();
            }
        }

        /// <summary>
        /// “Inside” places legend inside the grid area of the plot. 
        /// </summary>
        public Placement Placement
        {
            get
            {
                switch (GetString("placement"))
                {
                    case "outside": return Placement.Outside;
                    case "inside": return Placement.Inside;
                    default: return Placement.Inside;
                }
            }
            set
            {
                switch (value)
                {
                    case Placement.Inside: SetString("placement", "inside"); break;
                    case Placement.Outside: SetString("placement", "outside"); break;
                }
            }
        }
        
        /// <summary>
        /// Renderer specific options passed to the renderer
        /// </summary>
        /// <param name="rendererOptions"></param>
        /// <returns></returns>
        public LegendOptions SetRendererOptions(object rendererOptions)
        {
            this.rendererOptions = rendererOptions.ToString();

            return this;
        }
    }

    public enum Position{
        Bottom,
        Top,
        Right,
        Left,
        TopRight,
        TopLeft,
        BottomRight,
        BottomLeft
    }

    public enum Placement{
        Inside,
        Outside
    }
}
